package com.ftg.learn.controller;


import com.ftg.learn.service.TextToVideoService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 文本生成视频控制器
 * @author 布衣
 */
@RestController
@RequestMapping("/video")
public class TextToVideoController {

    private final TextToVideoService textToVideoService;

    public TextToVideoController(TextToVideoService textToVideoService) {
        this.textToVideoService = textToVideoService;
    }

    /**
     * 根据文本生成视频
     * @param prompt 文本提示词
     * @param size 视频分辨率，默认为1280*720
     * @return 任务ID
     */
    @PostMapping("/generate")
    public ResponseEntity<Map<String, String>> generateVideo(
            @RequestParam("prompt") String prompt,
            @RequestParam(value = "size", defaultValue = "1280*720") String size) {

        try {
            String taskId = textToVideoService.generateVideoTask(prompt, size);
            return ResponseEntity.ok(Map.of("taskId", taskId, "message", "Video generation task created successfully"));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(Map.of("error", "Failed to create video generation task: " + e.getMessage()));
        }
    }

    /**
     * 根据任务ID查询视频生成结果
     * @param taskId 任务ID
     * @return 视频生成结果
     */
    @GetMapping("/result/{taskId}")
    public ResponseEntity<Map<String, String>> getVideoResult(@PathVariable String taskId) {
        try {
            Map<String, String> result = textToVideoService.getVideoResult(taskId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(Map.of("error", "Failed to get video generation result: " + e.getMessage()));
        }
    }
}

